import axios from 'axios'
import {getToken} from '../utils/auth'

const instance = axios.create({
  baseURL: 'http://localhost:5000/',
  timeout: 5000,
})

instance.interceptors.request.use(
  (config)=>{
    console.log(config);
    let token=getToken()
    if(token){
      config.headers['Authorization']='Bearer '+ token
    }
    
    return config
  },(err)=>{
    console.log(err);
    return Promise.reject(err)
  }
)

instance.interceptors.response.use(
  (response)=>{
    console.log(response);
    if(response.status===200){
      return response.data
    }
    return response
  },(err)=>{
    console.log(err);
    return Promise.reject(err)
  }
)

export default instance